package ListDemo;

/**
 * 通过冒泡排序给数组进行排序
 * array1[]={1,5,3,2,4}
 * 结果：array1[]={1,2,3,4,5}
 */
public class Demo3 {
    public static void main(String[] args) {
        int[] array1 = {1, 5, 3, 2, 4};
        System.out.println("之前array1=");
        printArray(array1);
        int length = array1.length;

        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length - 1 - i; j++) {
                if (array1[j] > array1[j + 1]) {
                    int temp = array1[j];
                    array1[j] = array1[j + 1];
                    array1[j + 1] = temp;
                }
            }
        }
        System.out.println("之后array1=");
        printArray(array1);
    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
}
